package com.clw.practice.io.okhttp;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkHttpDemo2 {
	public static void main(String[] args) throws Exception {
		String access_token = "5s3hzUY4o-k0T-qBqbU06lxjsti7kTEV5aDknviZRPxhdlONOK1h9mnFEfsPoYqxdOXtmGdW3pZFouNYj_p29ETMSjaoKFqCQHCZU1h-585xkqFRTs3-CI0JZuUywBM-w77qrNSta6NGNjAnYgusieVTRbwWOzoXfgo5aQDu-bO9BnveNJr5Kpinxsx3scSD0kWwo_i5FBu6HUGesMU3nELCSjUBURLAqkGqRzG2nS8";
		String media_id = "1077R2SZIgS-d7G_Ghib_9EpMuKfqvM2PwdXvZqiKmMeSfmm5YOuVMLlFWJBC_9Ms";
		
		OkHttpClient client = new OkHttpClient();
        Request.Builder builder = new Request.Builder()
        .url("https://xtbglpt.digitalgd.com.cn/ebus/yzy_y0mue86hu1/cgi-bin/media/get?access_token="+access_token+"&media_id="+media_id)
        .post(null);
        Request.Builder builderNew = builder
        .addHeader("Content-Type", "application/json");
        
        Map<String, String> headers = addHeaders("yzy_oal1je4wnb", "2TuNNDlbnrrwHbuA37mQbtzGn3caGzzW");
        for (Map.Entry<String, String> entry : headers.entrySet()) {
        	builderNew.addHeader(entry.getKey(), entry.getValue());
        }
        Request request = builderNew.build();
        Response response = client.newCall(request).execute();
        System.out.println(response.code());
	}

	private static Map<String, String> addHeaders(String passId, String passToken) {
		LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC);
		long timestamp = now.atZone(ZoneOffset.UTC).toEpochSecond();
		String nonce = UUID.randomUUID().toString();
		String signature = getSign(timestamp, passToken, nonce);
		Map<String, String> headerMap = new HashMap<String, String>();
		headerMap.put("x-rio-paasid", passId);
		headerMap.put("x-rio-signature", signature);
		headerMap.put("x-rio-timestamp", String.valueOf(timestamp));
		headerMap.put("x-rio-nonce", nonce);
		return headerMap;
	}

	public static String getSign(long timestamp, String passToken, String nonce) {

		String formatString = String.format("%s%s%s%s", timestamp, passToken, nonce, timestamp);
		MessageDigest messageDigest;
		String encodeStr = null;
		try {
			messageDigest = MessageDigest.getInstance("SHA-256");
			messageDigest.update(formatString.getBytes(StandardCharsets.UTF_8));
			encodeStr = byte2Hex(messageDigest.digest());
		} catch (NoSuchAlgorithmException e) {
			System.out.println(e.getMessage());
		}
		Objects.requireNonNull(encodeStr);
		return encodeStr.toUpperCase();
	}

	private static String byte2Hex(byte[] bytes) {
		StringBuilder stringBuffer = new StringBuilder();
		String temp;
		for (byte aByte : bytes) {
			temp = Integer.toHexString(aByte & 0xFF);
			if (temp.length() == 1) {
				stringBuffer.append("0");
			}
			stringBuffer.append(temp);
		}
		return stringBuffer.toString();
	}
}